<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Cache;

class ProductController extends Controller
{
    public function index(Request $request)
    {
    	//设置变量$arr为空
    	$arr = '';
    	//查询运动所有类型
    	$sdata = DB::table('sport') -> select('sport_type','sport_id') -> get();
    	//查询所有品牌类型
    	$bdata = DB::table('brand')->get();
    	//查询所有人群类型
    	$cdata = DB::table('crowd') -> get();
    	// dd($sdata);
    	//查询cro_id
    	$cro_id = $request -> cro_id;
    	//查询spo_id
    	$spo_id = $request -> spo_id;
    	//查询brand_id
    	$brand_id = $request -> brand_id;
    	// dd($brand_id);
    	 
    	//如果search有传值过来
    	if(!$request -> search)
    	{
    		//如果有人群id传递过来
    		if($request -> cro_id)
    		{
    			//如果没有运动类型id传递过来 
	    		if(!$request -> spo_id)
		    	{
		    		
		    		$crspr_id = DB::table('cro_spo_rel') -> where('cro_id',$request -> cro_id) -> lists('crspr_id');
		    		// dd($crspr_id);
		    	}elseif($request -> spo_id)
		    	{
		    		//有运动类型id传递过来
		    		$arr = 1;
		    		$crspr_id = DB::table('cro_spo_rel') -> where('cro_id',$request -> cro_id) -> where('spo_id',$request -> spo_id) -> lists('crspr_id');
		    		// dd($crspr_id);
		    		
		    	}

		    	//如果有品牌id传过来
		    	if($request -> brand_id)
		    	{
		    		$arr = 2;
		    		$data = DB::table('product') -> where('brand_id',$request -> brand_id) -> whereIn('crspr_id',$crspr_id) -> paginate(12);
		    		// dd($data);
		    		// 如果有运动id
		    		if($request -> spo_id)
		    		{
		    			$arr = 3;
		    		}
		    		
		    	}else{
		    		
		    		//如果没有品牌id传过来
		    		$data = DB::table('product') -> whereIn('crspr_id',$crspr_id) ->paginate(12);
		    		// dd($data);
		    	}
    		}else{

    			//如果有运动id传递过来
    			if($request -> spo_id)
    			{
    				$crspr_id = DB::table('cro_spo_rel') -> where('spo_id',$request -> spo_id) -> lists('crspr_id');
    				$data = DB::table('product') -> whereIn('crspr_id',$crspr_id) ->paginate(12);
    			}elseif($request -> brand_id)
    			{
    				$data = DB::table('product') -> where('brand_id',$request -> brand_id) -> paginate(12);
    			}
    			
    		}
    		
    	}else
    	{
    		
    		$data = DB::table('product') -> where('keyword','like','%'.$request -> search.'%') -> paginate(12);
    		
    	}
    	
    	// dd($arr);
    	return view('home/main/product',['data'=>$data,'request'=>$request -> all(),'search'=>$request->search,'arr'=>$arr,'sdata'=>$sdata,'bdata'=>$bdata,'cdata'=>$cdata,'cro_id'=>$cro_id,'spo_id'=>$spo_id,'brand_id'=>$brand_id]);
    	
	}

	

 
    public function show($proid)

    {
    	$product=DB::table('product')->where('product_id','=',$proid)->select('product_id','crspr_id','title','keyword','description','mprice','sprice','pre_pro_code')->first();
    	//var_dump($product);
    	//色系
    	$pro_colors=DB::table('color')->leftJoin('col_siz_rel','col_siz_rel.col_id','=','color.color_id')->leftJoin('pro_attr','pro_attr.cosir_id','=','col_siz_rel.cosir_id')->where('pro_attr.pro_id','=',$proid)->distinct()->select('color_id','color_hex_code','suf_pic')->get();
    	//var_dump($pro_colors);

    	//尺码
    	$sizes_arr=array();
    	for($i=0;$i<=count($pro_colors)-1;$i++){
    	 	$pro_sizes=DB::table('size')->leftJoin('col_siz_rel','col_siz_rel.siz_id','=','size.size_id')->leftJoin('pro_attr','pro_attr.cosir_id','=','col_siz_rel.cosir_id')->where('pro_attr.pro_id','=',$proid)->where('col_id','=',$pro_colors[$i]->color_id)->get();
    	 	//var_dump($pro_sizes);
    	 	$every_col_sizes_str='';
    	 	foreach($pro_sizes as $pro_size)
    	 	{
    	 		$every_col_sizes_str.='<li style="float:left;"><a class="siz" proid="'.$pro_size->pro_id.'" colid="'.$pro_size->col_id.'" keyword="'.$pro_size->keyword.'" sufprocode="'.$pro_size->suf_pro_code.'" cosirid="'.$pro_size->cosir_id.'" stocknum="'.$pro_size->attr_stocknum.'">'.$pro_size->size_size.'</a></li>';	
    	 	}
    	 	$sizes_arr[]=$every_col_sizes_str;
    	 	//var_dump($every_col_sizes_str);
    	}
    	//var_dump($sizes_arr);


    	//右侧搜索
    	//人群缓存
        if(!Cache::has('crowds'))
        {
            $crowds=DB::table('crowd')->get();
            Cache::forever('crowds',$crowds);
        }
        $crowds=Cache::get('crowds');
        //运动缓存
        if(!Cache::has('sports'))
        {
            $sports=DB::table('sport')->get();
            Cache::forever('sports',$sports);
        }
        $sports=Cache::get('sports');
        //品牌缓存
        if(!Cache::has('allbrands'))
        {
            $allbrands=DB::table('brand')->get();
            Cache::put('allbrands',$allbrands,2);
        }
        $allbrands=Cache::get('allbrands');
        //var_dump($crowds);
        //var_dump($sports);
        //var_dump($allbrands);

        //相似商品
        $crspr_id=$product->crspr_id;
        //var_dump($crspr_id);
       	$same_products=DB::table('product')->where('product_id','<>',$proid)->where('crspr_id','=',$crspr_id)->orderBy('product_id','desc')->take(4)->get();
       	//var_dump($same_products); 
    	return view('home/main/single',['product'=>$product,'pro_colors'=>$pro_colors,'sizes_arr'=>$sizes_arr,'crowds'=>$crowds,'sports'=>$sports,'allbrands'=>$allbrands,'same_products'=>$same_products]);
    }
    public function chicknum($proid)
    {
    	DB::table('product')->where('product_id','=',$proid)->increment('chicknum',1);
    	return redirect()->route('single',[$proid]);
    }
}
